package com.slot.faraohgoldview;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.view.View;

public class TopPanel extends View {
	private float scaleY, scaleX;
	static final int LINES = 9;
	static final int line_figs = 6;
	static final int[] LINE_H = { 100, 100, 100, 300, 300, 200, 200, 300, 300 };
	static final int[] LINE_Y = { 175, 75, 275, 75, 75, 175, 75, 75, 75 };
	static final int[] LINE0_LY = { 214, 110, 317, 75, 352, 180, 249, 145, 284 };
	static final int[] LINE0_RY = { 214, 110, 317, 75, 352, 180, 249, 284, 145 };
	private int[] LineFigNow = { 1, 0, 0, 0, 0, 0, 0, 0, 0 };
	static final int[][] LINE_COLOR = {
			{ Color.rgb(40, 58, 147), Color.rgb(114, 172, 255) },
			{ Color.rgb(89, 20, 9), Color.rgb(255, 23, 0) },
			{ Color.rgb(2, 16, 0), Color.rgb(61, 144, 20) },
			{ Color.rgb(94, 81, 0), Color.rgb(255, 255, 0) },
			{ Color.rgb(81, 0, 106), Color.rgb(255, 65, 255) },
			{ Color.rgb(30, 64, 15), Color.rgb(147, 255, 22) },
			{ Color.rgb(96, 85, 68), Color.rgb(255, 255, 255) },
			{ Color.rgb(89, 20, 9), Color.rgb(242, 130, 0) },
			{ Color.rgb(53, 76, 91), Color.rgb(114, 255, 255) } };
	static final int[][][] drawLine = {
			{
					{ 0, 225, 640, 225 },
					{ 0, 225, 42, 225, 140, 225, 157, 225, 255, 225, 640, 225 },
					{ 0, 225, 42, 225, 140, 225, 157, 225, 255, 225, 271, 225,
							370, 225, 640, 225 },
					{ 0, 225, 42, 225, 140, 225, 157, 225, 255, 225, 271, 225,
							370, 225, 385, 225, 483, 225, 640, 225 },
					{ 0, 225, 42, 225, 140, 225, 157, 225, 255, 225, 271, 225,
							370, 225, 385, 225, 483, 225, 498, 225, 598, 225,
							640, 225 }, },
			{
					{ 0, 125, 640, 125 },
					{ 0, 125, 42, 125, 140, 125, 157, 125, 255, 125, 640, 125 },
					{ 0, 125, 42, 125, 140, 125, 157, 125, 255, 125, 271, 125,
							370, 125, 640, 125 },
					{ 0, 125, 42, 125, 140, 125, 157, 125, 255, 125, 271, 125,
							370, 125, 385, 125, 483, 125, 640, 125 },
					{ 0, 125, 42, 125, 140, 125, 157, 125, 255, 125, 271, 125,
							370, 125, 385, 125, 483, 125, 498, 125, 598, 125,
							640, 125 }, },
			{
					{ 0, 325, 640, 325 },
					{ 0, 325, 42, 325, 140, 325, 157, 325, 255, 325, 640, 325 },
					{ 0, 325, 42, 325, 140, 325, 157, 325, 255, 325, 271, 325,
							370, 325, 640, 325 },
					{ 0, 325, 42, 325, 140, 325, 157, 325, 255, 325, 271, 325,
							370, 325, 385, 325, 483, 325, 640, 325 },
					{ 0, 325, 42, 325, 140, 325, 157, 325, 255, 325, 271, 325,
							370, 325, 385, 325, 483, 325, 498, 325, 598, 325,
							640, 325 }, },
			{
					{ 0, 90, 50, 90, 50, 90, 320, 325, 320, 325, 590, 90, 590,
							90, 640, 90 },
					{ 0, 90, 42, 90, 140, 166, 157, 184, 255, 270, 320, 325,
							320, 325, 590, 90, 590, 90, 640, 90 },
					{ 0, 90, 42, 90, 140, 166, 157, 184, 255, 270, 271, 285,
							370, 285, 590, 90, 590, 90, 640, 90 },
					{ 0, 90, 42, 90, 140, 166, 157, 184, 255, 270, 271, 285,
							370, 285, 385, 270, 483, 184, 590, 90, 590, 90,
							640, 90 },
					{ 0, 90, 42, 90, 140, 166, 157, 184, 255, 270, 271, 285,
							370, 285, 385, 270, 483, 184, 498, 166, 598, 90,
							640, 90 } },
			{
					{ 0, 360, 50, 360, 50, 360, 320, 125, 320, 125, 590, 360,
							590, 360, 640, 360 },
					{ 0, 360, 42, 360, 140, 283, 157, 267, 255, 180, 320, 125,
							320, 125, 590, 360, 590, 360, 640, 360 },
					{ 0, 360, 42, 360, 140, 283, 157, 267, 255, 180, 271, 165,
							370, 165, 590, 360, 590, 360, 640, 360 },
					{ 0, 360, 42, 360, 140, 283, 157, 267, 255, 180, 271, 165,
							370, 165, 385, 180, 483, 267, 590, 360, 590, 360,
							640, 360 },
					{ 0, 360, 42, 360, 140, 283, 157, 267, 255, 180, 271, 165,
							370, 165, 385, 180, 483, 267, 498, 283, 598, 360,
							640, 360 } },
			{
					{ 0, 190, 80, 190, 80, 190, 180, 350, 180, 350, 460, 350,
							460, 350, 570, 190, 570, 190, 640, 190 },
					{ 0, 190, 42, 190, 140, 261, 157, 287, 255, 350, 460, 350,
							460, 350, 570, 190, 570, 190, 640, 190 },
					{ 0, 190, 42, 190, 140, 261, 157, 287, 255, 350, 271, 350,
							370, 350, 460, 350, 460, 350, 570, 190, 570, 190,
							640, 190 },
					{ 0, 190, 42, 190, 140, 261, 157, 287, 255, 350, 271, 350,
							370, 350, 385, 350, 483, 287, 570, 190, 570, 190,
							640, 190 },
					{ 0, 190, 42, 190, 140, 261, 157, 287, 255, 350, 271, 350,
							370, 350, 385, 350, 483, 287, 498, 261, 598, 190,
							640, 190 } },
			{
					{ 0, 260, 80, 260, 80, 260, 180, 100, 180, 100, 460, 100,
							460, 100, 570, 260, 570, 260, 640, 260 },
					{ 0, 260, 42, 260, 140, 190, 157, 165, 255, 100, 460, 100,
							460, 100, 570, 260, 570, 260, 640, 260 },
					{ 0, 260, 42, 260, 140, 190, 157, 165, 255, 100, 271, 100,
							370, 100, 460, 100, 460, 100, 570, 260, 570, 260,
							640, 260 },
					{ 0, 260, 42, 260, 140, 190, 157, 165, 255, 100, 271, 100,
							370, 100, 385, 100, 483, 165, 570, 260, 570, 260,
							640, 260 },
					{ 0, 260, 42, 260, 140, 190, 157, 165, 255, 100, 271, 100,
							370, 100, 385, 100, 483, 165, 498, 190, 598, 260,
							640, 260 }, },
			{
					{ 0, 160, 240, 160, 240, 160, 400, 295, 400, 295, 640, 295 },
					{ 0, 160, 42, 160, 140, 160, 157, 160, 255, 166, 400, 295,
							400, 295, 640, 295 },
					{ 0, 160, 42, 160, 140, 160, 157, 160, 255, 166, 271, 183,
							370, 265, 400, 295, 400, 295, 640, 295 },
					{ 0, 160, 42, 160, 140, 160, 157, 160, 255, 166, 271, 183,
							370, 265, 385, 283, 483, 295, 640, 295 },
					{ 0, 160, 42, 160, 140, 160, 157, 160, 255, 166, 271, 183,
							370, 265, 385, 283, 483, 295, 498, 295, 598, 295,
							640, 295 } },
			{
					{ 0, 295, 240, 295, 240, 295, 400, 160, 400, 160, 640, 160 },
					{ 0, 295, 42, 295, 140, 295, 157, 295, 255, 285, 400, 160,
							400, 160, 640, 160 },
					{ 0, 295, 42, 295, 140, 295, 157, 295, 255, 285, 271, 268,
							370, 185, 400, 160, 400, 160, 640, 160 },
					{ 0, 295, 42, 295, 140, 295, 157, 295, 255, 285, 271, 268,
							370, 185, 385, 169, 483, 160, 640, 160 },
					{ 0, 295, 42, 295, 140, 295, 157, 295, 255, 285, 271, 268,
							370, 185, 385, 169, 483, 160, 498, 160, 598, 160,
							640, 160 } } };
	static final Bitmap[] SYMBOL = new Bitmap[13];
	static final Bitmap[] EYE_ANIM = new Bitmap[18];
	private int bet = 1;
	private Bitmap[] line = new Bitmap[LINES];
	private RectF[] rect = new RectF[9];
	private RectF[] rectL = new RectF[9];
	private RectF[] rectR = new RectF[9];
	private Bitmap autoBitmap, mTpBitmap, FGtablet, FGtop;
	private boolean isAuto = false;
	private boolean isFGtop = false;
	private int FGtabletframe = 0;
	private int fgwin;
	private Paint paint, painttablet, painttop, paintline;

	public TopPanel(Context context,float scaleX,float scaleY) {
		super(context);

		paint = new Paint();
		paint.setColor(Color.BLACK);
		paint.setTextAlign(Paint.Align.CENTER);
		paint.setTypeface(Typeface.DEFAULT_BOLD);
		painttablet = new Paint();
		painttablet.setColor(Color.YELLOW);
		painttop = new Paint();
		painttop.setColor(Color.YELLOW);
		painttop.setTextAlign(Paint.Align.CENTER);

		paintline = new Paint();
		paintline.setAntiAlias(true);
		paintline.setColor(Color.rgb(114, 172, 255));

		mTpBitmap = BitmapFactory.decodeResource(context.getResources(),
				R.drawable.tpanelelite);
		autoBitmap = BitmapFactory.decodeResource(context.getResources(),
				R.drawable.auto);
		FGtablet = BitmapFactory.decodeResource(context.getResources(),
				R.drawable.fgtablet);
		FGtop = BitmapFactory.decodeResource(context.getResources(),
				R.drawable.fgtop);

		int idl[] = { R.drawable.line0_0, R.drawable.line1_0,
				R.drawable.line2_0, R.drawable.line3_0, R.drawable.line4_0,
				R.drawable.line5_0, R.drawable.line6_0, R.drawable.line7_0,
				R.drawable.line8_0 };
		for (int i = 0; i < idl.length; i++) {
			line[i] = BitmapFactory.decodeResource(context.getResources(),
					idl[i]);
		}
		int ids[] = { R.drawable.sim0, R.drawable.sim1, R.drawable.sim2,
				R.drawable.sim3, R.drawable.sim4, R.drawable.sim5,
				R.drawable.sim7, R.drawable.sim8, R.drawable.sim9,
				R.drawable.sim10, R.drawable.sim11, R.drawable.sim12,
				R.drawable.sim6 };
		for (int i = 0; i < ids.length; i++) {
			SYMBOL[i] = BitmapFactory.decodeResource(context.getResources(),
					ids[i]);
		}

		int idy[] = { R.drawable.eye0, R.drawable.eye1, R.drawable.eye2,
				R.drawable.eye3, R.drawable.eye4, R.drawable.eye5,
				R.drawable.eye6, R.drawable.eye7, R.drawable.eye8,
				R.drawable.eye9, R.drawable.eye10, R.drawable.eye11,
				R.drawable.eye12, R.drawable.eye13, R.drawable.eye14,
				R.drawable.eye15, R.drawable.eye16, R.drawable.eye17 };
		for (int i = 0; i < idy.length; i++) {
			EYE_ANIM[i] = BitmapFactory.decodeResource(context.getResources(),
					idy[i]);
		}
		symbolResize(scaleX, scaleY);

	}

	@Override
	protected void onSizeChanged(int width, int height, int oldw, int oldh) {
		scaleY = (float) height / 480;
		scaleX = (float) width / 640;
		paint.setTextSize(20 * scaleY);
		painttablet.setTextSize(30 * scaleY);
		painttablet.setAntiAlias(true);
		painttop.setTextSize(25 * scaleY);
		mTpBitmap = Bitmap.createScaledBitmap(mTpBitmap, width, height, true);
		autoBitmap = Bitmap.createScaledBitmap(autoBitmap, (int) (49 * scaleX),
				(int) (18 * scaleY), true);
		FGtablet = Bitmap.createScaledBitmap(FGtablet, (int) (429 * scaleX),
				(int) (200 * scaleY), true);
		FGtop = Bitmap.createScaledBitmap(FGtop, width, (int) (76 * scaleY),
				true);
		for (int i = 0; i < LINES; i++) {
			rect[i] = new RectF(0, LINE_Y[i] * scaleY, width,
					(LINE_Y[i] + LINE_H[i]) * scaleY);
			rectL[i] = new RectF(0, LINE0_LY[i] * scaleY, 27 * scaleX,
					LINE0_LY[i] * scaleY + 25 * scaleY);
			rectR[i] = new RectF(width - 27 * scaleX, LINE0_RY[i] * scaleY,
					width, LINE0_RY[i] * scaleY + 25 * scaleY);
		}
		symbolResize(scaleX, scaleY);

	}

	@Override
	public void onDraw(Canvas canvas) {
		super.onDraw(canvas);

		canvas.drawBitmap(mTpBitmap, 0, 0, null);
		if (isFGtop) {
			canvas.drawBitmap(FGtop, 0, 0, null);
			canvas.drawText("" + fgwin, 320 * scaleX, 50 * scaleY, painttop);
		}
		if (isAuto)
			canvas.drawBitmap(autoBitmap, 575 * scaleX, 20 * scaleY, null);

		for (int i = 0; i < 9; i++) {
			if (LineFigNow[i] > 0) {
				paintline.setStrokeWidth(5 * scaleY);
				paintline.setColor(LINE_COLOR[i][0]);
				if (LineFigNow[i] > 1) {
					float[] fdrawLine = new float[drawLine[i][LineFigNow[i] - 2].length];
					for (int j = 0; j < fdrawLine.length; j++) {
						if (j % 2 == 0)
							fdrawLine[j] = (float) (drawLine[i][LineFigNow[i] - 2][j])
									* scaleX;
						else
							fdrawLine[j] = (float) (drawLine[i][LineFigNow[i] - 2][j])
									* scaleY;
					}

					canvas.drawLines(fdrawLine, paintline);
					if (LineFigNow[i] > 2)
						drawWinFrame(canvas, i, LineFigNow[i] - 1, paintline);
					paintline.setColor(LINE_COLOR[i][1]);
					paintline.setStrokeWidth(3 * scaleY);
					canvas.drawLines(fdrawLine, paintline);
					if (LineFigNow[i] > 2)
						drawWinFrame(canvas, i, LineFigNow[i] - 1, paintline);
				}

				canvas.drawBitmap(line[i], null, rectL[i], null);
				canvas.drawBitmap(line[i], null, rectR[i], null);
				canvas.drawText("" + bet, 15 * scaleX, 20 * scaleY
						+ LINE0_LY[i] * scaleY, paint);
				canvas.drawText("" + bet, 625 * scaleX, 20 * scaleY
						+ LINE0_RY[i] * scaleY, paint);
			}
		}
		if (FGtabletframe > 0) {
			canvas.drawBitmap(FGtablet, 105 * scaleX, 125 * scaleY, null);
			if (FGtabletframe == 1) {
				canvas.drawText("YOU WIN 15 FREE GAMES", 180 * scaleX,
						200 * scaleY, painttablet);
				canvas.drawText("ALL PRIZES x3", 240 * scaleX, 250 * scaleY,
						painttablet);
			} else if (FGtabletframe == 2) {
				canvas.drawText("FEATURE WIN", 240 * scaleX, 200 * scaleY,
						painttablet);
				canvas.drawText(fgwin + " CREDITS", 240 * scaleX, 250 * scaleY,
						painttablet);
			}
		}

	}

	public void drawWinFrame(Canvas canvas, int line, int fig, Paint paint) {
		float topleftx, toplefty, btmrightx, btmrighty;
		for (int i = 0; i < fig; i++) {
			topleftx = FaraohGoldView.SHIFT_REELS_Y[i] * scaleX;
			toplefty = 275 * scaleY - 100 * CheckWin.LINE[line][i] * scaleY;
			btmrightx = topleftx + 100 * scaleX;
			btmrighty = toplefty + 100 * scaleY;
			canvas.drawLines(new float[] { topleftx, toplefty, topleftx,
					btmrighty, topleftx, btmrighty, btmrightx, btmrighty,
					btmrightx, btmrighty, btmrightx, toplefty, btmrightx,
					toplefty, topleftx, toplefty }, paint);
		}
	}

	public void setLines(int[] lines) {
		LineFigNow = lines;
		invalidate();
	}

	public void setBet(int b) {
		bet = b;
		invalidate();
	}

	public void setAuto(boolean isauto) {
		isAuto = isauto;
		invalidate();
	}

	public void setFGtop(boolean isfgtop, int win) {
		isFGtop = isfgtop;
		fgwin = win;
		invalidate();
	}

	public void setFGtablet(int fgtablet) {
		FGtabletframe = fgtablet;
		invalidate();
	}
	
	private void symbolResize(float scaleX, float scaleY){
		for (int i = 0; i < SYMBOL.length; i++) {
			SYMBOL[i] = Bitmap.createScaledBitmap(SYMBOL[i],
					(int) (100 * scaleX), (int) (100 * scaleY), true);
		}
		for (int i = 0; i < EYE_ANIM.length; i++) {
			EYE_ANIM[i] = Bitmap.createScaledBitmap(EYE_ANIM[i],
					(int) (100 * scaleX), (int) (100 * scaleY), true);
		}
	}

}
